<template>
  <div>
    <el-button type="primary" @click="dialogTableVisible = true">添加</el-button>
    <el-dialog v-el-drag-dialog :visible.sync="dialogTableVisible" title="添加" width="900px">
      <el-form ref="row_data" :model="row_data" :rules="rules" label-position="left" label-width="100px" style="width: 750px; margin-left:50px;">
        <el-form-item :label="'商家名称'" prop="merchantName">
          <el-input v-model="row_data.merchantName" placeholder="商家名称"/>
        </el-form-item>
        <el-form-item :label="'申请提款时间'" prop="applySpendingTime">
          <el-date-picker v-model="row_data.applySpendingTime" type="datetime" value-format="timestamp" placeholder="选择申请提款时间"/>
        </el-form-item>
        <el-form-item :label="'提现状态'" prop="state">
          <el-input v-model="row_data.state" placeholder="提现状态"/>
        </el-form-item>
        <el-form-item :label="'到账时间'" prop="accountingTime">
          <el-date-picker v-model="row_data.accountingTime" type="datetime" value-format="timestamp" placeholder="选择到账时间"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm()">立即创建</el-button>
          <el-button @click="close()">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import elDragDialog from '@/directive/el-dragDialog' // base on element-ui
import { add } from '@/api/merchant/merchant_spending_record'
import store from '@/store'

export default {
  name: 'DragDialogDemo',
  directives: { elDragDialog },
  data() {
    return {
      rules: {
        merchantName: [
          { required: true, message: '商家名称不能为空', trigger: 'blur' }
        ],
        applySpendingTime: [
          { type: 'date', required: true, message: '请选择申请提款时间', trigger: 'blur' },
          { required: true, message: '申请提款时间不能为空', trigger: 'blur' }
        ],
        state: [
          { required: true, message: '提现状态不能为空', trigger: 'blur' }
        ],
        accountingTime: [
          { type: 'date', required: true, message: '请选择到账时间', trigger: 'blur' },
          { required: true, message: '到账时间不能为空', trigger: 'blur' }
        ]
      },
      dialogTableVisible: false,
      row_data: {}
    }
  },
  methods: {
    submitForm() {
      this.$refs.row_data.validate(valid => {
        if (valid) {
          const data = this.row_data
          data.applySpendingTime = parseInt(data.applySpendingTime / 1000)
          data.accountingTime = parseInt(data.accountingTime / 1000)
          add(store.getters.token, data).then(res => {
            if (res.status) {
              this.$message.success('添加成功！')
              this.$emit('getLists')
              this.close()
            } else {
              this.$message.error('添加失败！原因：' + res.msg)
            }
          })
        } else {
          return false
        }
      })
    },
    close() {
      this.dialogTableVisible = false
    }
  }
}
</script>

